package com.yun.util.yun.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class LoginInterceptor implements HandlerInterceptor {

    private Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);

    @Autowired
    private Environment environment;

    //在业务处理器处理请求之前被调用。预处理，可以进行编码、安全控制、权限校验等处理
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        HttpSession session = request.getSession();
        Object obj = session.getAttribute("userName");
        String requestURL = request.getRequestURL().toString();
        logger.info("请求路径:{}", requestURL);
        if (obj == null) {
            String sysParam = request.getParameter("sys_");
            logger.info("sys_:{}",sysParam);
            if (sysParam!=null && sysParam.equals("ipran")) {//所有ipran的页面,如果没有登录,需要到mis的认证页面认证
                String misAuthorize = environment.getProperty("mis_authorize");
                String misClientId = environment.getProperty("mis_client_id");
                logger.info("准备跳转:{}",misAuthorize + "?response_type=code&client_id="+misClientId+"&state=" + requestURL);
                response.sendRedirect(misAuthorize + "?response_type=code&client_id="+misClientId+"&state=" + requestURL);
            } else {
                response.sendRedirect(request.getContextPath() + "/login");
            }
            return false;// 必须返回false，不然业务会被执行，存在漏洞
        }
        return true;// 只有返回true才会继续向下执行，返回false取消当前请求
    }

    //在业务处理器处理请求执行完成后，生成视图之前执行。后处理（调用了Service并返回ModelAndView，但未进行页面渲染），有机会修改ModelAndView （这个博主就基本不怎么用了）
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        request.setAttribute("_js_version", System.currentTimeMillis());
    }

    //在DispatcherServlet完全处理完请求后被调用，可用于清理资源等。返回处理（已经渲染了页面）
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {

    }
}
